package com.hchun;

import com.hchun.dao.IUserDao;
import com.hchun.io.Resources;
import com.hchun.pojo.User;
import com.hchun.sqlSession.SqlSession;
import com.hchun.sqlSession.SqlSessionFactory;
import com.hchun.sqlSession.SqlSessionFactoryBuilder;
import com.hchun.test.IPersistenceTest;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Test {

    public static void main(String[] args) throws Exception {
        // 获取字节输入流
        InputStream resourceAsSteam = Resources.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        // 创建sqlSession会话
        SqlSession sqlSession = sqlSessionFactory.openSession();

        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        System.out.println(userDao.findAll());
        // 调用
//        User user = new User(1,"hchun");
//        User user2 = sqlSession.selectOne("user.selectOne",user);
//        System.out.println(sqlSession.selectList("user.selectList"));
//        InputStream in =  Test.class.getClassLoader().getResourceAsStream("sqlMapConfig.xml");
//        System.out.println(in == null);
//        Connection connection = null;
//        PreparedStatement preparedStatement = null;
//        ResultSet resultSet = null;
//        try{
//            // 加载数据库驱动
//            Class.forName("com.mysql.jdbc.Driver");
//            connection =
//                    DriverManager.getConnection("jdbc:mysql://106.12.148.100:3306/mybatisPra?characterEncoding=utf-8","root","root");
//            // 定义sql语句 ?表示占位符
//            String sql = "select * from user where username = ?";
//            // 获取预处理statement
//            preparedStatement = connection.prepareStatement(sql);
//            // 设置参数，第一个参数为sql语句，第二个开始填问好
//            preparedStatement.setString(1,"tom");
//            // 遍历结果
//            while (resultSet.next()){
//                int id = resultSet.getInt("id");
//                String username = resultSet.getString("username");
//                System.out.println(id);
//                System.out.println(username);
//            }
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            System.out.println("=========");
//        }
    }

}
